package com.weimob.mcloud.wechatwork.customer.mq.product;

import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.CorpTagInfo;
import com.weimob.mcloud.wechatwork.customer.constant.RedisLockConstant;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisBaseInfo;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisConstant;
import com.weimob.mcloud.wechatwork.customer.redis.RedisService;
import com.weimob.mcloud.wechatwork.customer.service.impl.ArtemisService;
import com.weimob.mcloud.wechatwork.customer.service.user.enums.tag.TagChangeEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.msgBody.CorpCustomerTagChangeBody;
import com.weimob.mcloud.wechatwork.customer.service.user.module.msgBody.TagChangeBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2022/3/24 7:41 下午
 */
@Slf4j
@Service
public class TagChangeMsgProduct {

    @Autowired
    private ArtemisService artemisService;

    @Autowired
    private RedisService redisService;

    public void sendTagCreateMsg(Long bosId, String corpId, String groupId, String groupName, List<CorpTagInfo> corpTagInfoList) {

        for (CorpTagInfo corpTagInfo : corpTagInfoList) {
            //发送企微官方标签变更数据,目前只有cdp在消费，如果后续有其他业务在使用，建议在这里说明
            TagChangeBody tagChangeBody = new TagChangeBody();
            tagChangeBody.setBosId(bosId);
            tagChangeBody.setCorpId(corpId);
            tagChangeBody.setCorpTagGroupId(groupId);
            tagChangeBody.setTagGroupName(groupName);
            tagChangeBody.setCorpTagId(corpTagInfo.getTagId());
            tagChangeBody.setTagName(corpTagInfo.getTagName());
            tagChangeBody.setTagChangeType(TagChangeEnum.TAG_CREATE.getType());
            try {
                sendTagChangeMsg(tagChangeBody);
            } catch (Exception e) {
                log.error("同步企微官方标签创建, 发送消息失败, msgBody:{}, error:{}", JSON.toJSONString(tagChangeBody), e);
            }
        }

    }

    public void sendTagDeleteMsg(Long bosId, String corpId, String groupId, String tagId, Integer tagChangeType) {
        //发送企微官方标签变更数据,目前只有cdp在消费，如果后续有其他业务在使用，建议在这里说明
        TagChangeBody tagChangeBody = new TagChangeBody();
        tagChangeBody.setBosId(bosId);
        tagChangeBody.setCorpId(corpId);
        tagChangeBody.setCorpTagGroupId(groupId);
        tagChangeBody.setCorpTagId(tagId);
        tagChangeBody.setTagChangeType(tagChangeType);
        try {
            sendTagChangeMsg(tagChangeBody);
        } catch (Exception e) {
            log.error("同步企微官方标签创建, 发送消息失败, msgBody:{}, error:{}", JSON.toJSONString(tagChangeBody), e);
        }
    }

    /**
     * 标签数据变更消息发送
     * 1、标签创建标签编辑
     * 2、标签组名称更新
     * 3、标签名称更新
     * 4、标签组删除
     * 5、标签删除
     * @param tagChangeBody
     */
    public void sendTagChangeMsg(TagChangeBody tagChangeBody) {

        //加幂等控制
        String loseKey = String.format(RedisLockConstant.TAG_CHANGE_KEY, tagChangeBody.getCorpId(), tagChangeBody.getBosId(),
                tagChangeBody.getCorpTagGroupId(), tagChangeBody.getCorpTagId(), tagChangeBody.getTagChangeType(),
                tagChangeBody.getTagGroupName(), tagChangeBody.getTagName());

        try {
            boolean dumpFlag = redisService.setNotExists(loseKey, "30", RedisLockConstant.lockTime, TimeUnit.SECONDS);
            if (!dumpFlag) {
                log.info("标签变更消息以重复, request:{}", JSON.toJSONString(tagChangeBody));
                return;
            }
        } catch (Exception e) {
            log.warn("标签变更消息发送幂等失败, e", e);
            //幂等抛异常了，就不做幂等，允许重复发送
        }


        ArtemisBaseInfo<TagChangeBody> artemisBaseInfo = new ArtemisBaseInfo<>();
        artemisBaseInfo.setTopic(ArtemisConstant.Topic.WECHATWORK_TAG_CHANGE_TOPIC);
        artemisBaseInfo.setEvent(tagChangeBody);
        artemisBaseInfo.setProducer(ArtemisConstant.USER);

        log.info("企微官方标签数据变更消息发送, magBody:{}", JSON.toJSONString(artemisBaseInfo));
        artemisService.sendToArtemis(artemisBaseInfo,tagChangeBody.getBosId());

    }

    /**
     * 企微官方"员工---客户---标签"关系数据变更消息
     * @param corpCustomerTagChangeBody
     */
    public void sendCorpCustomerTagChangeBody(CorpCustomerTagChangeBody corpCustomerTagChangeBody) {

        if (Objects.isNull(corpCustomerTagChangeBody) || Objects.isNull(corpCustomerTagChangeBody.getWid())) {
            log.error("发送客户标签变更消息失败, wid不允许为空, request:{}", JSON.toJSONString(corpCustomerTagChangeBody));
            return;
        }

        ArtemisBaseInfo<CorpCustomerTagChangeBody> artemisBaseInfo = new ArtemisBaseInfo<>();
        artemisBaseInfo.setTopic(ArtemisConstant.Topic.WECHATWORK_CUSTOMER_TAG_CHANGE_TOPIC);
        artemisBaseInfo.setEvent(corpCustomerTagChangeBody);
        artemisBaseInfo.setProducer(ArtemisConstant.USER);

        log.info("企微官方客户标签数据变更消息发送, magBody:{}", JSON.toJSONString(artemisBaseInfo));
        artemisService.sendToArtemis(artemisBaseInfo, corpCustomerTagChangeBody.getBosId());
    }

}
